/************************************************************************************************************************
 Name of the class: RequestServlet.java
 
 Author: Saiaswin Saikannan
 
 Role:
 This servlet is called when there is a HTTP Post request to the URL Pattern * /request/. This class calls the respective
 functions to send the email to the user and also to add details to the database.
 
 ***********************************************************************************************************************/


package com.request;

import java.io.IOException;
import java.util.Random;
import javax.servlet.http.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;
import java.io.PrintWriter;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class RequestServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name = req.getParameter("name");
		String description = req.getParameter("feedback");
		if (description == "" || description == null)
			description = "NA";
		String university = req.getParameter("university");
		String email = req.getParameter("email");

		try {
			sendEmail(name, email, description, university);
		} catch (Exception ex) {
			resp.sendRedirect("error.html");
		}

		try {
			addEntry(name, university, email, description);

		} catch (Exception ex) {
			resp.sendRedirect("error.html");
		}

		resp.sendRedirect("thankyou.jsp");
	}

	protected void sendEmail(String name, String email, String description,
			String university) throws Exception {

		if (description == "NA")
			description = "";

		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		String msgBody = "Hi "
				+ name
				+ ",\n\nThe pdf copy of the book can be downloaded from "
				+ "http://tinyurl.com/downloadpdf3e \nPlease let me know if you "
				+ "face issues downloading the file.\n\nI hope you find the book useful. "
				+ "Feedback most welcome :-).\n\nAll the best.\nDamith C. Rajapakse (Dr) ; Senior Lecturer, School of Computing ; COM2-2-57, 13 Computing Drive, Singapore 117417 ;\n"
				+ "65-6516 4359 (Office) ; 65-6779 4580 (Fax) ; damith@comp.nus.edu.sg  (E) ; http://www.comp.nus.edu.sg/~damithch (W)\n\n\n"
				+ "----------------------\nFor your record, this is what you submitted:\n"
				+ "Name: " + name + "\nEmail: " + email + "\nUniversity: "
				+ university + "\nPurpose of request/Feedback: " + description
				+ "\n----------------------";

		try {
			String email1 = "s.saiaswin@gmail.com";
			String name1 = "Damith";
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("mail2saiaswin@gmail.com",
					"Damith C.Rajapakse"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
					email, name));
			msg.addRecipient(Message.RecipientType.CC, new InternetAddress(
					email1, name1));
			msg.setSubject("Request for the pdf file of the complete book "
					+ "[Practical Tips for Software-Intensive Student Projects]");
			msg.setText(msgBody);
			Transport.send(msg);

		} catch (Exception e) {
			throw e;
		}
	}

	protected void addEntry(String name, String university, String email,
			String description) throws Exception {
		Date dt = new Date();
		Long id = System.nanoTime();
		Request d = new Request(id, name, university, email, dt, description);
		try {
			DBUtils.insert(d);
		} catch (Exception ex) {
			throw ex;
		}
	}
}
